package edu.sctu.lab03;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

public class UserDeleteServerlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        String id = req.getParameter("id");

// 3 +1
        Connection connection = null;
        Statement statement = null;

// t_users(id, username, password) id自动增加
        String sql = "DELETE FROM  t_users WHERE  id = " + id;

        try {
            Class.forName("com.mysql.jdbc.Driver");

        connection = DriverManager.getConnection(
                "jdbc:mysql://localhost:3306/sctu_javaweb_2018?serverTimezone=UTC",
                "root",
                "123456"
        );
        statement = connection.createStatement();
        boolean resultSet = statement.execute(sql);
        statement.close();
        connection.close();
            resp.sendRedirect("http://localhost:8080/userList");

        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
